package com.leyou.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.entity.ClientInfo;
import com.leyou.auth.mapper.ClientMapper;
import com.leyou.auth.service.ClientService;
import com.leyou.common.exceptions.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author 虎哥
 */
@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, ClientInfo> implements ClientService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Value("${ly.jwt.key}")
    private String key;
    
    @Override
    public String getSecretKey(String clientId, String secret) {
        // 1.先根据clientId查询数据
        ClientInfo clientInfo = query().eq("client_id", clientId).one();
        // 2.判断是否存在
        if (clientInfo == null) {
            // 不存在，clientId错误
            throw new LyException(400, "客户端信息有误，再捣乱我就报警了！");
        }
        // 3.校验secret是否正确
        if (!passwordEncoder.matches(secret, clientInfo.getSecret())) {
            // 密码错误
            throw new LyException(400, "客户端信息有误，再捣乱我就报警了！");
        }
        // 4.返回秘钥
        return key;
    }
}